package com.educal.Controller;

import com.educal.Bean.Course;
import com.educal.Bean.Stu_Cou;
import com.educal.Bean.Student;
import com.educal.Repository.CourseRepository;
import com.educal.Repository.Stu_CouRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wzx on 2017/12/6.
 */
@RestController
public class CourseController {

    @Autowired
    private CourseRepository courseRepository;
    @Autowired
    private Stu_CouRepository stu_couRepository;

    @PostMapping(value = "/course")
    public Map<String, Object> courseAdd( @RequestParam("stuid") String stuid,
                                          @RequestParam("courseid") String courseid,
                                          @RequestParam("coursename") String coursename,
                                          @RequestParam("startime") String startime,
                                          @RequestParam("startweek") String startweek,
                                          @RequestParam("endtime") String endtime,
                                          @RequestParam("endweek") String endweek,
                                          @RequestParam("courseteacher") String courseteacher) {
        Course course = new Course();
        Stu_Cou stu_cou = new Stu_Cou();

        Map<String, Object> result = new HashMap<String, Object>();
        course.setCourseid(courseid);
        course.setCoursename(coursename);
        course.setStartime(startime);
        course.setEndtime(endtime);
        course.setStartweek(startweek);
        course.setEndweek(endweek);
        course.setCourseteacher(courseteacher);
        course = courseRepository.save(course);
        stu_cou.setCourseid(courseid);
        stu_cou.setStuid(stuid);
        stu_couRepository.save(stu_cou);
        if(course!=null) {
            result.put("code", 200);
            result.put("stu", course);
        }else{
            result.put("code", 400);
        }
        //System.out.print(stu.getStuid()+ stu.getStuname());
        return result;
    }
    @GetMapping(value = "/course")
    public Map<String, Object> findAll() {
        Map<String, Object> result = new HashMap<String, Object>();
        List<Course> courseList = courseRepository.findAll();
        if(courseList!=null&&!courseList.isEmpty()){
            result.put("code",200);
            result.put("courseList",courseList);
        }else{
            result.put("code",400);
        }
        return result;
    }
    /**
     * id查询
     */
    @GetMapping(value = "/course/{courseid}")
    public Map<String, Object> findByTeacherId(@PathVariable("courseid") String courseid) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code",400);
        Course course = new Course();
        List<Course> list = courseRepository.findAll();
        for(int i=0;i<list.size();i++){
            String cId = list.get(i).getCourseid();
            if(cId.equals(courseid)) {
                course.setCourseid(courseid);
                course.setCoursename(list.get(i).getCoursename());
                course.setStartime(list.get(i).getStartime());
                course.setEndtime(list.get(i).getEndtime());
                course.setStartweek(list.get(i).getStartweek());
                course.setEndweek(list.get(i).getEndweek());
                course.setCourseteacher(list.get(i).getCourseteacher());
                result.put("code", 200);
                result.put("course", course);
                break;
            }
        }
        return result;
    }
    /**
     * name查询
     */
    @GetMapping(value = "/course/name/{coursename}")
    public Map<String, Object> findByCourseName(@PathVariable("coursename") String coursename) {
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("code",400);
        List<Course> couList = new ArrayList<>();
        List<Course> list = courseRepository.findAll();
        for(int i=0;i<list.size();i++){
            String cName = list.get(i).getCoursename();
            if(cName.equals(coursename)){
                couList.add(list.get(i));
            }
        }
        if(couList!=null&&!couList.isEmpty()){
            result.put("code",200);
            result.put("course",couList);
        }
        // result.put("stuList",stuList);
        return result;
    }

    /**
     * teacher查询
     */
    @GetMapping(value = "/course/teacher/{teacher}")
    public Map<String, Object> findByTeacherName(@PathVariable("teacher") String teacher) {
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("code",400);
        List<Course> couTeaList = new ArrayList<>();
        List<Course> list = courseRepository.findAll();
        for(int i=0;i<list.size();i++){
            String cTeacher = list.get(i).getCourseteacher();
            if(cTeacher.equals(teacher)){
                couTeaList.add(list.get(i));
            }
        }
        if(couTeaList!=null&&!couTeaList.isEmpty()){
            result.put("code",200);
            result.put("course",couTeaList);
        }
        // result.put("stuList",stuList);
        return result;
    }

    /**
     * 删除
     */
    @DeleteMapping(value = "/course/{courseid}")
    public Map<String, Object> deleteCourse(@PathVariable("courseid") String courseid){
        Course course = new Course();
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("code",400);
        List<Course> list = courseRepository.findAll();
        for(int i=0;i<list.size();i++){
            String cId = list.get(i).getCourseid();
            if(cId.equals(courseid)){
                course.setCourseid(courseid);
                course.setCoursename(list.get(i).getCoursename());
                course.setStartime(list.get(i).getStartime());
                course.setEndtime(list.get(i).getEndtime());
                course.setStartweek(list.get(i).getStartweek());
                course.setEndweek(list.get(i).getEndweek());
                course.setCourseteacher(list.get(i).getCourseteacher());
                courseRepository.delete(course);
                result.put("code",200);
                break;
            }
        }
        return result;
    }

    /**
     * 更新
     *
     */
    @PutMapping(value = "/course")
    public Map<String, Object> courseUpdate(@RequestParam("stuid") String stuid,
                                @RequestParam("courseid") String courseid,
                               @RequestParam("coursename") String coursename,
                               @RequestParam("startime") String startime,
                               @RequestParam("startweek") String startweek,
                               @RequestParam("endtime") String endtime,
                               @RequestParam("endweek") String endweek,
                               @RequestParam("courseteacher") String courseteacher) {
        Course course = new Course();
        Stu_Cou stu_cou = new Stu_Cou();
        Map<String, Object> result = new HashMap<String, Object>();
        course.setCoursename(coursename);
        course.setCourseid(courseid);
        course.setStartime(startime);
        course.setEndtime(endtime);
        course.setStartweek(startweek);
        course.setEndweek(endweek);
        course.setCourseteacher(courseteacher);
        course = courseRepository.save(course);
        stu_cou.setCourseid(courseid);
        stu_cou.setStuid(stuid);
        stu_couRepository.save(stu_cou);
        if(course!=null) {
            result.put("code", 200);
            result.put("stu", course);
        }else{
            result.put("code", 400);
        }
        //System.out.print(stu.getStuid()+ stu.getStuname());
        return result;
    }
}
